package cz.muni.fi.hauntedhouses.dao.impl;

import cz.muni.fi.hauntedhouses.dao.HouseDAO;
import cz.muni.fi.hauntedhouses.entity.House;
import cz.muni.fi.hauntedhouses.to.HouseTO;
import cz.muni.fi.hauntedhouses.util.HouseUtil;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Jan Volmut, 324836
 */
@Repository
public class HouseDAOImpl implements HouseDAO {
    
    private EntityManager em;
    
    @PersistenceContext
    public void setEntityManagerFactory(EntityManager em) {
        this.em = em;
    }
    
    @Override
    public void create(HouseTO house) {
        if(house == null) {
            throw new IllegalArgumentException("House is null");
        }
        String houseName = house.getName();
        if(houseName.length() > 20) {
            house.setName(houseName.substring(20));
        }
        House house_dao = HouseUtil.toDAO(house);

        this.em.persist(house_dao);
        house.setId(house_dao.getId());
    }

    @Override
    public HouseTO get(Long id) {
        if(id == null || id < 1) {
            throw new IllegalArgumentException("Id cannot be less than 1");
        }
        
        House house = this.em.find(House.class, id);
        HouseTO house_to = HouseUtil.toDTO(house);
        return house_to;
    }

    @Override
    public void update(HouseTO house) {
        if(house == null) {
            throw new IllegalArgumentException("House is null");
        }
        House house_dao = HouseUtil.toDAO(house);
        this.em.merge(house_dao);
    }

    @Override
    public void delete(HouseTO house) {
        if(house == null || house.getId() == null) {
            throw new IllegalArgumentException("House or id is null");
        }
        
        House house_dao = HouseUtil.toDAO(house);
        House savedHouse = this.em.getReference(House.class, house_dao.getId());
        try {
        this.em.remove(savedHouse);
        } catch(PersistenceException ex) {
            throw new IllegalArgumentException();
        }
    }

    @Override
    public List<HouseTO> findAll() {
        List<House> resultList = this.em.createQuery("SELECT h FROM House h")
                                    .getResultList();
        
        List<HouseTO> resultList_to = HouseUtil.listToDTO(resultList);
        
        return resultList_to;
    }

    @Override
    public List<HouseTO> findByName(String name) {
        Query query = this.em.createQuery(
                "SELECT h FROM House h where name = :name",
                House.class
                );
        
        query.setParameter("name", name);
        List<HouseTO> resultList = HouseUtil.listToDTO(query.getResultList());
        
        return resultList;
    }
    
}
